﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Library.Helpers
{
    public static class NullConverter
    {        
        public static string FromDbToString(object value)
        {
            if (value == null || value == DBNull.Value)
                return null;
            else
                return value.ToString();
        }

        public static string FromDbToStringTrim(object value)
        {
            if (value == null || value == DBNull.Value)
                return null;
            else
                return value.ToString().Trim();
        }

        public static Decimal FromDbToDecimal(object value)
        {
            if (value == null || value == DBNull.Value)
                return 0;
            else
                return Convert.ToDecimal(value);
        }

        public static double FromDbToDouble(object value)
        {
            if (value == null || value == DBNull.Value)
                return 0;
            else
                return Convert.ToDouble(value);
        }

        public static short FromDbToInt16(object value)
        {
            if (value == null || value == DBNull.Value)
                return 0;
            else
                return Convert.ToInt16(value);
        }

        public static int FromDbToInt32(object value)
        {
            if (value == null || value == DBNull.Value)
                return 0;
            else
                return Convert.ToInt32(value);
        }

        public static Int64 FromDbToInt64(object value)
        {
            if (value == null || value == DBNull.Value)
                return 0;
            else
                return Convert.ToInt64(value);
        }


        public static System.Guid FromDbToGuid(object value)
        {
            if (value == null || value == DBNull.Value)
            {
                return Guid.Empty;
            }
            else
                return new Guid(value.ToString());
        }

        public static bool FromDbToBoolean(object value)
        {
            if (value == null || value == DBNull.Value)
                return false;
            else
                return Convert.ToBoolean(value);
        }

        public static DateTime? FromDBToNullableDateTime(object value)
        {
            if (value == null || value == DBNull.Value)
                return null;
            else
                return Convert.ToDateTime(value);
        }

        public static double? FromDBToNullableDouble(object value)
        {
            if (value == null || value == DBNull.Value)
                return null;
            else
                return Convert.ToDouble(value);
        }

        public static Int16? FromDBToNullableInt16(object value)
        {
            if (value == null || value == DBNull.Value)
                return null;
            else
                return Convert.ToInt16(value);
        }

        public static int? FromDBToNullableInt32(object value)
        {
            if (value == null || value == DBNull.Value)
                return null;
            else
                return Convert.ToInt32(value);
        }

        public static Int64? FromDBToNullableInt64(object value)
        {
            if (value == null || value == DBNull.Value)
                return null;
            else
                return Convert.ToInt64(value);
        }

        public static object ToNullObject(int? value)
        {
            if (value.HasValue)
                return value.Value;
            else
                return null;
        }

        public static object ToNullObject(DateTime? value)
        {
            if (value.HasValue)
                return value.Value;
            else
                return null;
        }

        public static object ToNullObject(double? value)
        {
            if (value.HasValue)
                return value.Value;
            else
                return null;
        }

        public static string ToNullStringTrim(string objectValue)
        {
            if (string.IsNullOrEmpty(objectValue))
                return null;
            else
                return objectValue.Trim();
        }

        public static string ToEmptyStringTrim(string objectValue)
        {
            if (string.IsNullOrEmpty(objectValue))
                return string.Empty;
            else
                return objectValue.Trim();
        }

        public static int FromStringToInt32(string value)
        {
            int result;
            if (int.TryParse(value, out result))
                return result;
            else
                return 0;
        }
        public static Int64 FromStringToInt64(string value)
        {
            Int64 result;
            if (Int64.TryParse(value, out result))
                return result;
            else
                return 0;
        }

        public static Int16 FromStringToInt16(string value)
        {
            Int16 result;
            if (Int16.TryParse(value, out result))
                return result;
            else
                return 0;
        }

        public static Decimal FromStringToDecimal(string value)
        {
            Decimal result;
            if (Decimal.TryParse(value, out result))
                return result;
            else
                return 0;
        }

        public static float FromStringToFloat(string value)
        {
            float result;
            if (float.TryParse(value, out result))
                return result;
            else
                return 0;
        }

        public static bool FromStringToBoolean(string value)
        {
            bool result;
            if (bool.TryParse(value, out result))
                return result;

            if (value == "1") return true;
            if (value == "0") return false;

            return false;
        }

        public static bool FromCharToBoolean(char value)
        {
            if (value == '1') return true;
            if (value == '0') return false;

            return false;
        }

        public static double FromStringToDouble(string value)
        {
            double result;
            if (double.TryParse(value, out result))
                return result;
            else
                return 0;
        }

        public static object FromIntToIntOrDBNullObject(int value)
        {
            if (value != null)
                return value;
            else
                return DBNull.Value;
        }

        public static object FromStringToInt32OrDBNullObject(string value)
        {
            int result;
            if (int.TryParse(value, out result))
                return result;
            else
                return DBNull.Value;
        }

        public static object FromStringDefaultToInt32OrDBNullObject(string value)
        {
            if (value == "0")
                return DBNull.Value;

            return FromStringToInt32OrDBNullObject(value);
        }

        public static object FromStringToTrimStringOrDBNullObject(string value)
        {
            if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(value.Trim()))
                return DBNull.Value;
            else
                return value.Trim();
        }

        public static object ToObjectOrDBNull(object value)
        {
            if (value != null)
                return value;
            else
                return DBNull.Value;
        }

        public static object ToObjectOrDBNull(double? value)
        {
            if (value.HasValue)
                return value.Value;
            else
                return DBNull.Value;
        }

        public static object ToObjectOrDBNull(Int16? value)
        {
            if (value.HasValue)
                return value.Value;
            else
                return DBNull.Value;
        }

        public static object ToObjectOrDBNull(Int32? value)
        {
            if (value.HasValue)
                return value.Value;
            else
                return DBNull.Value;
        }

        public static DateTime? FromStringToNullableDateTime(string value)
        {
            DateTime result;
            if (DateTime.TryParse(value, out result))
                return result;
            else
                return null;
        }

        public static Int16? FromStringToNullableInt16(string value)
        {
            Int16 result;
            if (short.TryParse(value, out result))
                return result;
            else
                return null;
        }

        public static Int32? FromStringToNullableInt32(string value)
        {
            Int32 result;
            if (int.TryParse(value, out result))
                return result;
            else
                return null;
        }

        public static Int64? FromStringToNullableInt64(string value)
        {
            Int64 result;
            if (Int64.TryParse(value, out result))
                return result;
            else
                return null;
        }

        public static double? FromStringToNullableDouble(string value)
        {
            double result;
            if (double.TryParse(value, out result))
                return result;
            else
                return null;
        }

        public static decimal? FromStringToNullableDecimal(string value)
        {
            decimal result;
            if (decimal.TryParse(value, out result))
                return result;
            else
                return null;
        }

        public static string FromDbToDoubleAsString(object objectValue)
        {
            if ((objectValue == null) || (objectValue == DBNull.Value))
                return string.Empty;
            else
            {
                double? result = NullConverter.FromDBToNullableDouble(objectValue);
                if (result.HasValue)
                    return result.Value.ToString();
                else
                    return null;
            }
        }

        public static string FromDbToDateTimeString(object objectValue)
        {
            if ((objectValue == null) || (objectValue == DBNull.Value))
                return string.Empty;
            else
            {
                DateTime? result = NullConverter.FromDBToNullableDateTime(objectValue);
                if (result.HasValue)
                    return result.Value.ToString();
                else
                    return null;
            }
        }

        public static string FromDbToShortDateTimeString(object objectValue)
        {
            if ((objectValue == null) || (objectValue == DBNull.Value))
                return string.Empty;
            else
            {
                DateTime? result = NullConverter.FromDBToNullableDateTime(objectValue);
                if (result.HasValue)
                    return result.Value.ToShortDateString();
                else
                    return null;
            }
        }
    }    
}
